/*
 * @Author: taoke
 * @Date: 2022-05-11 10:42:42
 * @LastEditors: taoke
 * @LastEditTime: 2022-05-11 15:32:14
 * @Description: 列表相关工具
 * @FilePath: \yunying-sys-cli4\src\utils\table.js
 */
/**
 * @description: table值根据字典option格式化
 * @param {*} cellValue 需要转换的值
 * @param {*} condition 条件语句
 * @param {*} options 字典option
 * @return {*}
 */
export function formatterValueByDict({ cellValue, condition, options }) {
  const type = {
    key: 'dictLabel',
    value: 'dictValue'
  }
  return formatterValueCommon({
    cellValue,
    condition,
    options,
    type
  })
}

/**
 * @description: table值根据option格式化
 * @param {*} cellValue 需要转换的值
 * @param {*} condition 条件语句
 * @param {*} options option
 * @return {*}
 */
export function formatterValueByOptions({ cellValue, condition, options }) {
  return formatterValueCommon({
    cellValue,
    condition,
    options
  })
}

function formatterValueCommon({
  cellValue,
  condition,
  options,
  type = { key: 'label', value: 'value' }
}) {
  if (typeof condition === 'boolean' ? condition : cellValue) {
    const data = options.find((element) => element[type.value] === cellValue)
    if (data && Object.keys(data).length > 0) {
      return data[type.key]
    } else {
      return ''
    }
  } else {
    return ''
  }
}
